﻿using System;
using System.Reflection;
using Terraria;
using Terraria.ModLoader;

namespace 积分商店扩展.Common.ModUtils
{ 

	internal class MyUtils
	{
		public static Func<bool> GetDownedBossCondition(Mod mod, string typeName, string fieldName)
		{
			return delegate
			{
				try
				{
					Type type = mod.Code.GetType(typeName);
					if (type != null)
					{
						FieldInfo field = type.GetField(fieldName, BindingFlags.Static | BindingFlags.NonPublic);
						if (field != null)
						{
							return (bool)field.GetValue(null);
						}
					}
				}
				catch (Exception ex)
				{
					mod.Logger.Error("Error getting downed boss condition for " + fieldName + ": " + ex.Message);
				}
				return false;
			};
		}
		public static Func<Player, bool> GetBiomeActiveCondition(Mod mod, string biomeTypeName, string methodName)
		{
			return delegate(Player player)
			{
				try
				{
					Type biomeType = mod.Code.GetType(biomeTypeName);
					if (biomeType != null)
					{
						MethodInfo isBiomeActiveMethod = biomeType.GetMethod(methodName, new Type[] { typeof(Player) });
						if (isBiomeActiveMethod != null)
						{
							object biomeInstance = Activator.CreateInstance(biomeType);
							return (bool)isBiomeActiveMethod.Invoke(biomeInstance, new object[] { player });
						}
					}
				}
				catch (Exception ex)
				{
					mod.Logger.Error("Error checking " + biomeTypeName + " biome: " + ex.Message);
				}
				return false;
			};
		}
	}
}
